-- Init CTLD
local moose_ctld = CTLD:New(SIDE.BLUE, { "GRIFFIN" }, "GRIFFIN TRANSPORT")

moose_ctld:UnitCapabilities("AH-64D_BLK_II", true, true, 2, 2, 17, 8000)

-- Init Zones
moose_ctld:AddCTLDZone("GIRFFIN SOUTH FARP",
    CTLD.CargoZoneType.LOAD, SMOKECOLOR.Red, true, true
)

moose_ctld:AddCTLDZone("dropzone1",
    CTLD.CargoZoneType.DROP, SMOKECOLOR.Red, true, true
)

moose_ctld:AddCTLDZone("wpzone2",
    CTLD.CargoZoneType.MOVE, SMOKECOLOR.White, true, true
)

moose_ctld:ActivateZone("GRIFFIN SOUTH FARP", CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue)
moose_ctld:ActivateZone("dropzone1", CTLD.CargoZoneType.DROP, SMOKECOLOR.Blue)
moose_ctld:ActivateZone("wpzone2", CTLD.CargoZoneType.MOVE, SMOKECOLOR.Blue)


-- troops
moose_ctld:AddTroopsCargo("格里芬 FARP补给车队",
    { "GRIFFIN FARP Support" }, CTLD_CARGO.Enum.TROOPS, 4)

moose_ctld:AddTroopsCargo("格里芬 重装部队",
    { "GRIFFIN PLAYERTANK #001" }, CTLD_CARGO.Enum.TROOPS, 8)

moose_ctld:AddTroopsCargo("格里芬 近距防空部队",
    { "GRIFFIN SAM Lite" }, CTLD_CARGO.Enum.TROOPS, 7)

moose_ctld:AddTroopsCargo("格里芬 中距防空部队",
    { "GRIFFIN SAM Mid" }, CTLD_CARGO.Enum.TROOPS, 7)

moose_ctld:AddTroopsCargo("16LAB 工程师",
    { "GRIFFIN ENGINEER" }, CTLD_CARGO.Enum.ENGINEERS, 1)
moose_ctld.EngineerSearch = 2000

moose_ctld:AddTroopsCargo("格里芬 运输车",
    { "GRIFFIN PLAYERTRANSPORT-10" }, CTLD_CARGO.Enum.TROOPS, 1)

moose_ctld:AddTroopsCargo("格里芬 火炮部队",
    { "GRIFFIN Arty" }, CTLD_CARGO.Enum.TROOPS, 4)


-- FOB
moose_ctld:AddCratesCargo("FARP", { "GRIFFIN FARP SUPPORT #001-1" }, CTLD_CARGO.Enum.FOB, 3)

local FARPFreq = 130
local FARPName = 1 -- numbers 1..10

local FARPClearnames = {
    [1] = "London",
    [2] = "Dallas",
    [3] = "Paris",
    [4] = "Moscow",
    [5] = "Berlin",
    [6] = "Rome",
    [7] = "Madrid",
    [8] = "Warsaw",
    [9] = "Dublin",
    [10] = "Perth",
}

function BuildAFARP(Coordinate)
    local coord = Coordinate -- Core.Point#COORDINATE

    local FarpName = ((FARPName - 1) % 10) + 1
    local FName = FARPClearnames[FarpName]

    FARPFreq = FARPFreq + 1
    FARPName = FARPName + 1
    -- MessageToAll("初始化完成", 10, "DEBUG")
    -- Create a SPAWNSTATIC object from a template static FARP object.
    local SpawnStaticFarp = SPAWNSTATIC:NewFromStatic("GRIFFIN FARP-1", country.id.CJTF_BLUE)

    -- Spawning FARPs is special in DCS. Therefore, we need to specify that this is a FARP. We also set the callsign and the frequency.
    SpawnStaticFarp:InitFARP(FARPName, FARPFreq, 0)
    SpawnStaticFarp:InitDead(false)

    -- MessageToAll("初始化完成", 10, "DEBUG")

    -- Spawn FARP
    local ZoneSpawn = ZONE_RADIUS:New("FARP " .. FName, Coordinate:GetVec2(), 160, false)
    local Heading = 0
    local FarpBerlin = SpawnStaticFarp:SpawnFromZone(ZoneSpawn, Heading, "GRIFFIN FARP " .. FName)

    -- MessageToAll("正在建立", 10, "DEBUG")
    -- ATC and services - put them 125m from the center of the zone towards North
    local FarpVehicles = SPAWN:NewWithAlias("GRIFFIN FARP Support", "FARP " .. FName .. " Technicals")
    FarpVehicles:InitHeading(180)
    local FarpVCoord = coord:Translate(125, 0)
    FarpVehicles:SpawnFromCoordinate(FarpVCoord)
    -- MessageToAll("建立完成", 10, "DEBUG")

    -- We will put the rest of the statics in a nice circle around the center
    local base = 330
    local delta = 30

    -- local windsock = SPAWNSTATIC:NewFromStatic("Static Windsock-1",country.id.CJTF_BLUE)
    -- local sockcoord = coord:Translate(125,base)
    -- windsock:SpawnFromCoordinate(sockcoord,Heading,"Windsock "..FName)
    -- base=base-delta

    local fueldepot = SPAWNSTATIC:NewFromStatic("静态 补给点油库-2-1", country.id.CJTF_BLUE)
    local fuelcoord = coord:Translate(125, base)
    fueldepot:SpawnFromCoordinate(fuelcoord, Heading, "Fueldepot " .. FName)
    base = base - delta

    local ammodepot = SPAWNSTATIC:NewFromStatic("静态 补给点弹药仓库-2-1", country.id.CJTF_BLUE)
    local ammocoord = coord:Translate(125, base)
    ammodepot:SpawnFromCoordinate(ammocoord, Heading, "Ammodepot " .. FName)
    base = base - delta

    local CommandPost = SPAWNSTATIC:NewFromStatic("静态 补给点掩体指挥所-2-1", country.id.CJTF_BLUE)
    local CommandCoord = coord:Translate(125, base)
    CommandPost:SpawnFromCoordinate(CommandCoord, Heading, "Command Post " .. FName)
    base = base - delta

    local Tent1 = SPAWNSTATIC:NewFromStatic("静态 补给点帐篷-2-1", country.id.CJTF_BLUE)
    local Tent1Coord = coord:Translate(125, base)
    Tent1:SpawnFromCoordinate(Tent1Coord, Heading, "Command Tent " .. FName)
    base = base - delta

    local Tent2 = SPAWNSTATIC:NewFromStatic("静态 补给点帐篷-2-1", country.id.CJTF_BLUE)
    local Tent2Coord = coord:Translate(125, base)
    Tent2:SpawnFromCoordinate(Tent2Coord, Heading, "Command Tent2 " .. FName)

    -- add a loadzone to CTLD
    moose_ctld:AddCTLDZone("FARP " .. FName, CTLD.CargoZoneType.LOAD, SMOKECOLOR.Blue, true, true)
    local m = MESSAGE:New(string.format("FARP %s in operation!", FName), 15, "CTLD"):ToBlue()
end

function moose_ctld:OnAfterCratesBuild(From, Event, To, Group, Unit, Vehicle)
    local name = Vehicle:GetName()
    if string.match(name, "FARP", 1) then
        MessageToAll("格里芬后勤:前线燃油补给点建立完毕!", 10, "格里芬后勤")
        SOUND_ISAC_ONLINE:ToAll()
        local Coord = Vehicle:GetCoordinate()
        Vehicle:Destroy(false)
        BuildAFARP(Coord)
    end
end

-- options
moose_ctld.useprefix = true
moose_ctld.enableLoadSave = true
moose_ctld.saveinterval = 600
moose_ctld.filename = "missionsave.csv"
moose_ctld.eventoninject = true
moose_ctld.enableHercules = true
moose_ctld.enableHercules = true
moose_ctld.HercMinAngels = 155 -- for troop/cargo drop via chute in meters, ca 470 ft
moose_ctld.HercMaxAngels = 2000 -- for troop/cargo drop via chute in meters, ca 6000 ft
moose_ctld.HercMaxSpeed = 77 -- 77mps or 270kph or 150kn

moose_ctld:__Load(10)

moose_ctld:Start()